﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;

namespace Tasker.Common
{
    public static class DirectoryHelper
    {
        private const string DataFolderName = "Data";
        private const string LanguageDetectionModelsFolderName = "LanguageModels";
        private const string UserPhotosFolderName = "UserPhotos";
        private const string UserPhotoThumbFolderName = "Thumbs";
        private const string TempFolderName = "temp";

        static string _applicationDirectory;

        /// <summary>
        /// Current directory for web application.
        /// </summary>
        public static string CurrentDirectory
        {
            get
            {
                return
                    Environment.RunFromUnitTests
                  ? applicationDirectory			// not from web
                  : HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            }
        }

        /// <summary>
        /// Directory containing data such as SRX rules and so on.
        /// </summary>
        public static string DataDirectory
        {
            get
            {
                return Environment.RunFromWebServer
                    ? Path.Combine(DirectoryHelper.CurrentDirectory, string.Format(@"..\{0}\", DataFolderName))
                    : Path.Combine(DirectoryHelper.CurrentDirectory, string.Format(@"..\..\..\{0}\", DataFolderName));
            }
        }

        /// <summary>
        /// Directory with language models for language detection.
        /// </summary>
        public static string LanguageDetectionModelsDirectory
        {
            get { return Path.Combine(DataDirectory, LanguageDetectionModelsFolderName); }
        }

        /// <summary>
        /// Directory with user's photo
        /// </summary>
        public static string UserPhotosDirectory
        {
            get
            {
                return Path.Combine(DirectoryHelper.CurrentDirectory, string.Format(@"UserFiles\{0}\", UserPhotosFolderName));
            }
        }

        /// <summary>
        /// Directory with user's photo small copy
        /// </summary>
        public static string UserPhotoThumbsDirectory
        {
            get { return Path.Combine(UserPhotosDirectory, UserPhotoThumbFolderName); }
        }

        /// <summary>
        /// Folder with temporary data
        /// </summary>
        public static string TempFolder
        {
            get
            {
                return Path.Combine(DirectoryHelper.CurrentDirectory, string.Format(@"UserFiles\{0}\", TempFolderName));
            }
        }

        #region Private members
        static string applicationDirectory
        {
            get
            {
                var assembly = Assembly.GetEntryAssembly();
                assembly = assembly != null
                    ? assembly							// called from unmanaged application.
                    : Assembly.GetCallingAssembly();	// normal case

                if (string.IsNullOrEmpty(_applicationDirectory))
                    _applicationDirectory = Path.GetDirectoryName(assembly.Location) + "\\";
                return _applicationDirectory;
            }
        }
        #endregion
    }
}
